package com.example.back.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.back.dto.req.DoDisableReq;
import com.example.back.dto.req.DoctorListReq;
import com.example.back.dto.req.DoctorReq;
import com.example.back.dto.resp.DockerInfoRsp;
import com.example.back.dto.resp.DoctorListRsp;
import com.example.back.service.DoctorService;
import com.example.back.service.HospitalService;
import com.example.common.support.Result;
import com.example.common.utils.RequestHelp;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * @Author wxy
 * @Date 2021/1/14 13:35
 * @Version 1.0
 */
@RestController
@Api(tags = "基础信息管理-医生管理")
@RequestMapping("/api/Doctor")
public class DoctorController {

    @Resource
    private DoctorService doctorService;
    @Resource
    private HospitalService hospitalService;


    @ApiOperation(value = "医生列表", notes = "医生列表")
    @PostMapping(value = "/List")
    public Result<Map<String, Object>> List(@RequestBody DoctorListReq doctorListReq) {
        PageInfo<DoctorListRsp> doctorListRspPageInfo = doctorService.List(doctorListReq);
        Boolean isPlant = hospitalService.Info().getIsPlantOrder();
        Map<String, Object> m = new HashMap<>();
        m.put("list", doctorListRspPageInfo);
        m.put("isPlant", isPlant);
        return Result.success(m);
    }


    @ApiOperation(value = "启用、禁用", notes = "启用、禁用")
    @PostMapping(value = "/DoDisable")
    public Result<Object> DoDisable(@RequestBody DoDisableReq doDisableReq) {
        return Result.success(doctorService.DoDisable(doDisableReq));
    }

    @ApiOperation(value = "详情", notes = "详情")
    @PostMapping(value = "/Info")
    public Result<DockerInfoRsp> Info(@RequestBody DoDisableReq doDisableReq) {
        return Result.success(doctorService.Info(doDisableReq));
    }

    @ApiOperation(value = "上传文件", notes = "上传文件")
    @PostMapping(value = "/UpLoad")
    public Result<HashMap<String, Object>> UpLoad(@RequestParam(value = "file") MultipartFile file) {
        return Result.success(doctorService.UpLoad(file));
    }

    @ApiOperation(value = "保存", notes = "保存")
    @PostMapping(value = "/Save")
    public Result<Object> Save(@RequestBody DoctorReq doctorReq) {
        return Result.success(doctorService.Save(doctorReq));
    }

    //#region 排班
    @ApiOperation(value = "医生排班列表", notes = "医生排班列表")
    @PostMapping(value = "/PlantList")
    public Result<List<Map<String, List<Map<String, Object>>>>> PlantList(HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        long DCID = Jo.getLongValue("DCID");
        long timestamp = Jo.getLongValue("Month");
        Date Month = new Date(timestamp);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Month = format.parse(format.format(Month));
        } catch (Exception e) {
            //TODO: handle exception
        }
        
        List<Map<String, List<Map<String, Object>>>> list = doctorService.PlantList(DCID, Month);
        return Result.success(list);
    }

    @ApiOperation(value = "删除医生排班", notes = "删除医生排班")
    @PostMapping(value = "/DocPlantDel")
    public Result<Boolean> DocPlantDel(HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        long DCID = Jo.getLongValue("DCID");
        byte timeType = Jo.getByteValue("timeType");
        Date Day = Jo.getDate("Day");
                
        return Result.success(doctorService.DocPlantDel(DCID, timeType, Day));
    }

    @ApiOperation(value = "医生排班编辑", notes = "医生排班编辑")
    @PostMapping(value = "/DocPlantEdit")
    public Result<Boolean> DocPlantEdit(HttpServletRequest request) {
        JSONObject Jo = RequestHelp.GetFromBodyJson(request);
        List<Long> dcIdList = Jo.getJSONArray("dcIdList").toJavaList(Long.class);
        List<Byte> timeTypeList = Jo.getJSONArray("timeType").toJavaList(Byte.class); 
        Boolean isWeekEnd = Jo.getBoolean("WeekEnd");
        Date beg = Jo.getDate("beg");
        Date end = Jo.getDate("end");
        int countOrder = Jo.getIntValue("CountOrder");
        Boolean sess = true;
        try {
            sess = doctorService.DocPlantEdit(dcIdList, timeTypeList, isWeekEnd, beg, end, countOrder);
        } catch (Exception e) {
            //TODO: handle exception
        }
        
        return Result.success(sess); 
    }
    //#endregion
}

